package com.kangaroo.myhome.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kangaroo.myhome.model.TInspProjectPlan;
import com.kangaroo.myhome.service.impl.TInspProjectPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.kangaroo.myhome.Exception.CustomException;
import com.kangaroo.myhome.Exception.CustomExceptionType;
import com.kangaroo.myhome.model.TInspProject;
import com.kangaroo.myhome.model.TInspProjectExcel;
import com.kangaroo.myhome.service.impl.TInspProjectService;
import com.kangaroo.myhome.utils.ExcelExport2;
import com.kangaroo.myhome.vo.ResponseModel;

/**
 * @Description: 功能描述: //
 * @author: kangaroo
 * @date: 2020/7/31 0:02
 */
@RestController
@RequestMapping("projectPlan")
public class TInspProjectPlanController {

    @Autowired
    private TInspProjectPlanService tInspProjectPlanService;


   /* @GetMapping(value = "/findReportData")
    public ResponseModel findReportData(HttpServletRequest request) {
        ResponseModel model = new ResponseModel();
        String subway = request.getParameter("subway");
        return tInspProjectPlanService.findReportData(subway);
    }*/

    // @PreAuthorize("hasAuthority('sys:user:view')")
    @GetMapping(value = "/findAll")
    public ResponseModel findAll(HttpServletRequest request) {
        String projectId = request.getParameter("projectId");
        String pageS = request.getParameter("page");
        String rowsS = request.getParameter("rows");
        int page = 1;
        int rows = 10;
        if(pageS==null || "".equals(pageS)){

        }else{
            page = Integer.parseInt(pageS);
            rows = Integer.parseInt(rowsS);
        }

        return tInspProjectPlanService.findAll(projectId, page, rows);
    }

    /*@GetMapping(value = "/findAllJson")
    public ResponseModel findAllJson(HttpServletRequest request) {
        String subway = request.getParameter("subway");
        String cat = request.getParameter("cat");
        String degree = request.getParameter("degree");
        String stage = request.getParameter("stage");
        String name = request.getParameter("name");
        String pageS = request.getParameter("page");
        String rowsS = request.getParameter("rows");
        int page = Integer.parseInt(pageS);
        int rows = Integer.parseInt(rowsS);
        return tInspProjectPlanService.findAllJson(subway, cat, degree, stage,name, page, rows);
    }*/

    // @PreAuthorize("hasAuthority('sys:user:edit')")
    @PostMapping(value = "/edit")
    public ResponseModel edit(@RequestBody TInspProjectPlan tInspProjectPlan) {
        return tInspProjectPlanService.edit(tInspProjectPlan);
    }

    @PostMapping(value = "/editInfo")
    public ResponseModel editInfo(@RequestBody TInspProjectPlan tInspProjectPlan) {
        return tInspProjectPlanService.editInfo(tInspProjectPlan);
    }

    // @PreAuthorize("hasAuthority('sys:user:edit')")
    @GetMapping(value = "/view")
    public ResponseModel view(HttpServletRequest request) {
        ResponseModel model = new ResponseModel();
        String id = request.getParameter("id");

        TInspProjectPlan tInspProject = new TInspProjectPlan();
        tInspProject.setId(id);
        TInspProjectPlan user = tInspProjectPlanService.view(tInspProject);
        model.setCode(200);
        model.setMessage("获取成功");
        model.setData(user);
        return model;
    }

    // @PreAuthorize("hasAuthority('sys:user:delete')")
    @PostMapping(value = "/delete")
    public ResponseModel delete(@RequestParam  String id, HttpServletRequest request) {
        TInspProjectPlan tInspProject = new TInspProjectPlan();
        tInspProject.setId(id);
        tInspProjectPlanService.delete(tInspProject);
        ResponseModel model = new ResponseModel();
        model.setCode(200);
        model.setMessage("获取成功");
        return model;
    }

    /*@RequestMapping("/downloadProjectExcel")
    public void downloadProjectExcel(HttpServletRequest request, HttpServletResponse response) {
        // 序号	项目编号	项目名称	项目类型	所属线路	建设单位	项目阶段	收文时间	发文时间	办文时间(天)	函号	协调论证
        // 咨询意见	符合巡查	审核结果
        // 创建一个数组用于设置表头
        // String[] arr = new String[] {"ID", "项目编码", "项目名称", "项目类型", "所属路线"};
        String[] arr = new String[] {"ID", "项目编码", "项目名称", "项目类型", "所属路线",
                "建设单位","项目阶段","收文时间","发文时间","办文时间(天)","函号","协调论证",
                "咨询意见","符合巡查","审核结果"
        };
        String subway = request.getParameter("subway");
        String cat = request.getParameter("cat");
        String degree = request.getParameter("degree");
        String stage = request.getParameter("stage");
        List<TInspProject> projects = tInspProjectPlanService.findAllExcel(subway, cat, degree, stage);

        if(projects==null || projects.size()==0){
            throw new CustomException(CustomExceptionType.SYSTEM_ERROR,"\"暂无数据可以下载\"");
        }else{
            List<TInspProjectExcel> projectExcel = bulidProjectExcel(projects);
            // 调用Excel导出工具类
            ExcelExport2.export(response, projectExcel, arr);
        }
    }


    @RequestMapping("/downloadProjectExcelPath")
    public ResponseModel downloadProjectExcelPath(HttpServletRequest request, HttpServletResponse response) {
        ResponseModel model = new ResponseModel();
        // 序号	项目编号	项目名称	项目类型	所属线路	建设单位	项目阶段	收文时间	发文时间	办文时间(天)	函号	协调论证
        // 咨询意见	符合巡查	审核结果
        // 创建一个数组用于设置表头
        // String[] arr = new String[] {"ID", "项目编码", "项目名称", "项目类型", "所属路线"};
        String[] arr = new String[] {"ID", "项目编码", "项目名称", "项目类型", "所属路线",
                "建设单位","项目阶段","收文时间","发文时间","办文时间(天)","函号","协调论证",
                "咨询意见","符合巡查","审核结果"
        };
        String subway = request.getParameter("subway");
        String cat = request.getParameter("cat");
        String degree = request.getParameter("degree");
        String stage = request.getParameter("stage");
        List<TInspProject> projects = tInspProjectPlanService.findAllExcel(subway, cat, degree, stage);
        System.out.println("projects size = " + projects.size());
        String filePath = "";
        if(projects==null || projects.size()==0){
            // throw new CustomException(CustomExceptionType.SYSTEM_ERROR,"\"暂无数据可以下载\"");
            model.setCode(201);
            model.setData(filePath);
            model.setMessage("暂无数据可以下载");
            return model;
        }else{
            List<TInspProjectExcel> projectExcel = bulidProjectExcel(projects);
            // 调用Excel导出工具类
            filePath = ExcelExport2.exportPath(response, projectExcel, arr);
        }

        model.setCode(200);
        model.setData(filePath);
        model.setMessage("下载成功");

        return model;

    }

    private List<TInspProjectExcel> bulidProjectExcel(List<TInspProject> projects){
        List<TInspProjectExcel> projectExcels = new ArrayList<TInspProjectExcel>();
        int indexNo = 1;
        for(TInspProject project : projects){
            TInspProjectExcel projectExcel = new TInspProjectExcel();
            projectExcel.setId(indexNo+"");
            projectExcel.setCode(project.getCode());
            projectExcel.setName(project.getName());
            projectExcel.setCatName(project.getCatName());
            projectExcel.setSubwayName(project.getSubwayName());
            projectExcel.setConsCom(project.getConsCom());
            projectExcel.setStageName(project.getStageName());

            Object stageCons = project.getStageCons();
            System.out.println("stageCons size = " + stageCons);
            if(stageCons==null || "".equals(stageCons)){
                projectExcel.setSwTime(null);
                projectExcel.setFwTime(null);
                projectExcel.setBwDay("");
                projectExcel.setHNo("");
                projectExcel.setXtlz("");
                projectExcel.setZxyj("");
                projectExcel.setFhxc("");
                projectExcel.setScjg("");
            }else{
                //JSONObject.parseObject(stageCons)
                JSONObject stageConsJson = JSONObject.parseObject(stageCons.toString());
                if (stageConsJson!=null && !stageConsJson.isEmpty()){
                    projectExcel.setSwTime(stageConsJson.getDate("swTime"));
                    projectExcel.setFwTime(stageConsJson.getDate("fwTime"));
                    projectExcel.setBwDay(stageConsJson.getString("bwDay"));
                    projectExcel.setHNo(stageConsJson.getString("hNo"));
                    projectExcel.setXtlz(stageConsJson.getString("xtlz"));
                    projectExcel.setZxyj(stageConsJson.getString("zxyj"));
                    projectExcel.setFhxc(stageConsJson.getString("fhxc"));
                    projectExcel.setScjg(stageConsJson.getString("scjg"));
                }else{
                    projectExcel.setSwTime(null);
                    projectExcel.setFwTime(null);
                    projectExcel.setBwDay("");
                    projectExcel.setHNo("");
                    projectExcel.setXtlz("");
                    projectExcel.setZxyj("");
                    projectExcel.setFhxc("");
                    projectExcel.setScjg("");
                }
            }



            projectExcels.add(projectExcel);

        }
        return projectExcels;
    }*/

}